﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using YTSK.Model;
using System.Xml;
using System.Windows.Forms;

namespace YTSK.cSK.BW
{
    class cMakeXML_ZP
    {
        public static string MakeXML(Model.SK_SalebillVouch sk,
            List<Model.SK_SalebillVouchs_View> l_rds,out string savefile)
        {
            var path = System.Environment.GetEnvironmentVariable("TEMP");
            if (path.Substring(path.Length - 1, 1) != "\\")
            { path = path + "\\"; }
            savefile = $"{path}{sk.cCode}.xml";
        cSKSet:
            var _FHR = cSK.cSKSet.loadsave(SkConfig.复核人);
            var _SKR = cSK.cSKSet.loadsave(SkConfig.收款人);
            var _version = cSK.cSKSet.loadsave(SkConfig.税控分类版本号);
            if (_version.Length == 0)
            {
                MessageBox.Show("请先设置税控设备参数");
                var f = new FrmSKSet();
                f.ShowDialog();
                goto cSKSet;
            }


            var xml = new XmlDocument();
            var declare = xml.CreateXmlDeclaration("1.0", "GBK", null);
            xml.AppendChild(declare);
            var kp = xml.CreateElement("Kp");
            var version = xml.CreateElement("Verion");
            version.InnerText = "2.0";
            var fpxx = xml.CreateElement("Fpxx");
            var zsl = xml.CreateElement("Zsl");
            zsl.InnerText = "1";
            fpxx.AppendChild(zsl);
            var fpsj = xml.CreateElement("Fpsj");
            var fp = xml.CreateElement("Fp");
            var main = xml.CreateElement("Djh");
            main.InnerText = string.Format("{0}-{1}", sk.cCode, DateTime.Now.Second);
            fp.AppendChild(main);
            main = xml.CreateElement("Gfmc");
            main.InnerText = sk.cCusName;
            fp.AppendChild(main);
            main = xml.CreateElement("Gfsh");
            main.InnerText = sk.TaxNo;
            fp.AppendChild(main);
            main = xml.CreateElement("Gfyhzh");
            main.InnerText = sk.Bank.ObjToString() + " " + sk.BankAccount.ObjToString();
            fp.AppendChild(main);
            main = xml.CreateElement("Gfdzdh");
            main.InnerText = sk.Address.ObjToString() + " " + sk.Phone.ObjToString();
            fp.AppendChild(main);
            main = xml.CreateElement("Bz");
            main.InnerText = sk.cDf10;
            //main.InnerText = customer["Address"].ObjToString() + customer["Phone"].ObjToString();
            fp.AppendChild(main);
            main = xml.CreateElement("Fhr");
            main.InnerText = _FHR;
            fp.AppendChild(main);
            main = xml.CreateElement("Skr");
            main.InnerText = _SKR;
            fp.AppendChild(main);
            main = xml.CreateElement("Spbmbbh");
            main.InnerText = _version;
            fp.AppendChild(main);
            main = xml.CreateElement("Hsbz");
            main.InnerText = "0";
            fp.AppendChild(main);
            main = xml.CreateElement("Sgbz");
            main.InnerText = "0";
            fp.AppendChild(main);

            main = xml.CreateElement("Spxx");
            int irowcount = 1;
            for (int i = 0; i < l_rds.Count; i++)
            {
                var t = l_rds[i];
                if (t.cInvName.ObjToString().Length > 0)
                {
                    #region 非折扣行
                    if (t.iDiscountSum.ObjToDecimal() == 0)
                    {
                        var d = xml.CreateElement("Sph");
                        var detail = xml.CreateElement("Xh");
                        detail.InnerText = irowcount.ToString();
                        d.AppendChild(detail);
                        detail = xml.CreateElement("Spbm");
                        var cTaxType = t.cTaxType;
                        if (cTaxType.Length != 19)
                        { throw new Exception(string.Format("产品[{0}]的税收编码[{1}]不是是19位", t.cInvName, cTaxType)); }
                        detail.InnerText = cTaxType;
                        d.AppendChild(detail);
                        detail = xml.CreateElement("Qyspbm");
                        //detail.InnerText = "";
                        d.AppendChild(detail);
                        detail = xml.CreateElement("Syyhzcbz");
                        //detail.InnerText = "";
                        d.AppendChild(detail);
                        detail = xml.CreateElement("Yhzcsm");
                        //detail.InnerText = "";
                        d.AppendChild(detail);
                        detail = xml.CreateElement("Lslbz");
                        if (t.iTaxRateSale == 0)
                        { detail.InnerText = "2"; }
                        //detail.InnerText = "";
                        d.AppendChild(detail);
                        detail = xml.CreateElement("Spmc");
                        detail.InnerText = t.cInvName;
                        d.AppendChild(detail);
                        detail = xml.CreateElement("Ggxh");
                        detail.InnerText = t.cInvStd;
                        if (detail.InnerText.ObjToString().Length > 30)
                        {
                            detail.InnerText = detail.InnerText.Substring(0, 30);
                        }
                        d.AppendChild(detail);
                        detail = xml.CreateElement("Jldw");
                        detail.InnerText = t.cDW;
                        d.AppendChild(detail);
                        var jehs = t.iSumSale.ObjToDecimal();
                        var taxrate = t.iTaxRateSale.ObjToDecimal();
                        var jeori = jehs / (1 + taxrate / 100);
                        decimal jeinput = 0;
                        for (int m = 0; m <= 25; m++)
                        {
                            var tv = Math.Round(jeori, m);
                            var tv1 = Math.Round(tv * (1 + taxrate / 100), 2);
                            if (tv1 == jehs)
                            {
                                jeinput = tv;
                                break;
                            }
                            else
                            {
                                if (m == 25) { jeinput = tv; }
                            }
                        }
                        var sl = t.iQuantity;
                        jeinput = Math.Round(jeinput, 2);
                        var dj = Math.Round(t.iMoneySale.ObjToDecimal() / sl.ObjToDecimal(), 15, MidpointRounding.AwayFromZero);
                        detail = xml.CreateElement("Dj");
                        detail.InnerText = dj.ToString();
                        d.AppendChild(detail);
                        detail = xml.CreateElement("Sl");
                        detail.InnerText = sl.ObjToString();
                        d.AppendChild(detail);
                        detail = xml.CreateElement("Je");
                        detail.InnerText = t.iMoneySale.ObjToString();
                        d.AppendChild(detail);
                        detail = xml.CreateElement("Se");
                        detail.InnerText = t.iTaxSale.ObjToString();
                        d.AppendChild(detail);
                        detail = xml.CreateElement("Slv");
                        var itaxrate = Math.Round(t.iTaxRateSale.ObjToDecimal() / 100, 2);
                        detail.InnerText = itaxrate.ToString();
                        d.AppendChild(detail);
                        detail = xml.CreateElement("Kce");
                        //detail.InnerText = itaxrate.ToString();
                        d.AppendChild(detail);
                        main.AppendChild(d);
                        irowcount++;
                    }
                    #endregion
                    #region 开折扣
                    else
                    {
                        #region 原价行
                        var d = xml.CreateElement("Sph");
                        var detail = xml.CreateElement("Xh");
                        detail.InnerText = irowcount.ToString();
                        d.AppendChild(detail);
                        detail = xml.CreateElement("Spbm");
                        var cTaxType = t.cTaxType;
                        if (cTaxType.Length != 19)
                        { throw new Exception(string.Format("产品[{0}]的税收编码[{1}]不是是19位", t.cInvName, cTaxType)); }
                        detail.InnerText = cTaxType;
                        d.AppendChild(detail);
                        detail = xml.CreateElement("Qyspbm");
                        //detail.InnerText = "";
                        d.AppendChild(detail);
                        detail = xml.CreateElement("Syyhzcbz");
                        //detail.InnerText = "";
                        d.AppendChild(detail);
                        detail = xml.CreateElement("Yhzcsm");
                        //detail.InnerText = "";
                        d.AppendChild(detail);
                        detail = xml.CreateElement("Lslbz");
                        //detail.InnerText = "";
                        d.AppendChild(detail);
                        detail = xml.CreateElement("Spmc");
                        detail.InnerText = t.cInvName;
                        d.AppendChild(detail);
                        detail = xml.CreateElement("Ggxh");
                        detail.InnerText = t.cInvStd;
                        if (detail.InnerText.ObjToString().Length > 30)
                        {
                            detail.InnerText = detail.InnerText.Substring(0, 30);
                        }
                        d.AppendChild(detail);
                        detail = xml.CreateElement("Jldw");
                        detail.InnerText = t.cDW;
                        d.AppendChild(detail);
                        var jehs = t.iQuoteSum.ObjToDecimal();
                        var taxrate = t.iTaxRateSale.ObjToDecimal();
                        var jeori = jehs / (1 + taxrate / 100);
                        decimal jeinput = 0;
                        for (int m = 0; m <= 25; m++)
                        {
                            var tv = Math.Round(jeori, m);
                            var tv1 = Math.Round(tv * (1 + taxrate / 100), 2, MidpointRounding.AwayFromZero);
                            if (tv1 == jehs)
                            {
                                jeinput = tv;
                                break;
                            }
                            else
                            {
                                if (m == 25) { jeinput = tv; }
                            }
                        }
                        var se = jehs - jeinput;
                        var sl = t.iQuantity;
                        var dj = jeinput / sl;
                        if (t.iQuoteSum == t.iDiscountSum)
                        {
                            jeinput = Math.Round(jeinput, 2, MidpointRounding.AwayFromZero);
                            se = Math.Round(se, 2, MidpointRounding.AwayFromZero);
                        }
                        detail = xml.CreateElement("Dj");
                        detail.InnerText = dj.ToString();
                        d.AppendChild(detail);
                        detail = xml.CreateElement("Sl");
                        detail.InnerText = sl.ObjToString();
                        d.AppendChild(detail);
                        detail = xml.CreateElement("Je");
                        detail.InnerText = jeinput.ToString();
                        d.AppendChild(detail);
                        detail = xml.CreateElement("Se");
                        detail.InnerText = se.ToString();
                        d.AppendChild(detail);

                        detail = xml.CreateElement("Slv");
                        var itaxrate = Math.Round(t.iTaxRateSale.ObjToDecimal() / 100, 2);
                        detail.InnerText = itaxrate.ToString();
                        d.AppendChild(detail);
                        detail = xml.CreateElement("Kce");
                        //detail.InnerText = itaxrate.ToString();
                        d.AppendChild(detail);
                        main.AppendChild(d);
                        irowcount++;
                        #endregion

                        #region 折扣行
                        d = xml.CreateElement("Sph");
                        detail = xml.CreateElement("Xh");
                        detail.InnerText = irowcount.ToString();
                        d.AppendChild(detail);
                        detail = xml.CreateElement("Spbm");
                        cTaxType = t.cTaxType;
                        if (cTaxType.Length != 19)
                        { throw new Exception(string.Format("产品[{0}]的税收编码[{1}]不是是19位", t.cInvName, cTaxType)); }
                        detail.InnerText = cTaxType;
                        d.AppendChild(detail);
                        detail = xml.CreateElement("Qyspbm");
                        //detail.InnerText = "";
                        d.AppendChild(detail);
                        detail = xml.CreateElement("Syyhzcbz");
                        //detail.InnerText = "";
                        d.AppendChild(detail);
                        detail = xml.CreateElement("Yhzcsm");
                        //detail.InnerText = "";
                        d.AppendChild(detail);
                        detail = xml.CreateElement("Lslbz");
                        //detail.InnerText = "";
                        d.AppendChild(detail);
                        detail = xml.CreateElement("Spmc");
                        detail.InnerText = t.cInvName;
                        d.AppendChild(detail);
                        detail = xml.CreateElement("Ggxh");
                        detail.InnerText = t.cInvStd;
                        if (detail.InnerText.ObjToString().Length > 30)
                        {
                            detail.InnerText = detail.InnerText.Substring(0, 30);
                        }
                        d.AppendChild(detail);
                        detail = xml.CreateElement("Jldw");
                        detail.InnerText = t.cDW;
                        d.AppendChild(detail);
                        jehs = -t.iQuoteSum.ObjToDecimal();
                        taxrate = t.iTaxRateSale.ObjToDecimal();
                        jeori = jehs / (1 + taxrate / 100);
                        jeinput = 0;
                        for (int m = 0; m <= 25; m++)
                        {
                            var tv = Math.Round(jeori, m);
                            var tv1 = Math.Round(tv * (1 + taxrate / 100), 2, MidpointRounding.AwayFromZero);
                            if (tv1 == jehs)
                            {
                                jeinput = tv;
                                break;
                            }
                            else
                            {
                                if (m == 25) { jeinput = tv; }
                            }
                        }
                        if (t.iQuoteSum == t.iDiscountSum)
                        {
                            jeinput = Math.Round(jeinput, 2, MidpointRounding.AwayFromZero);
                            se = Math.Round(se, 2, MidpointRounding.AwayFromZero);
                        }
                        se = jehs - jeinput;
                        sl = t.iQuantity;
                        dj = jeinput / sl;
                        detail = xml.CreateElement("Dj");
                        //detail.InnerText = dj.ToString();
                        d.AppendChild(detail);
                        detail = xml.CreateElement("Sl");
                        //detail.InnerText = sl.ObjToString();
                        d.AppendChild(detail);
                        detail = xml.CreateElement("Je");
                        detail.InnerText = jeinput.ToString();
                        d.AppendChild(detail);
                        detail = xml.CreateElement("Se");
                        detail.InnerText = se.ToString();
                        d.AppendChild(detail);
                        detail = xml.CreateElement("Slv");
                        itaxrate = Math.Round(t.iTaxRateSale.ObjToDecimal() / 100, 2);
                        detail.InnerText = itaxrate.ToString();
                        d.AppendChild(detail);
                        detail = xml.CreateElement("Kce");
                        //detail.InnerText = itaxrate.ToString();
                        d.AppendChild(detail);
                        main.AppendChild(d);
                        irowcount++;
                        #endregion

                    }
                    #endregion
                }
            }
            fp.AppendChild(main);
            fpsj.AppendChild(fp);
            kp.AppendChild(version);
            fpxx.AppendChild(fpsj);
            kp.AppendChild(fpxx);
            xml.AppendChild(kp);
            if (System.IO.File.Exists(savefile))
            { System.IO.File.Delete(savefile); }
            xml.Save(savefile);
            return savefile;
        }
    }
}
